﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using DevExpress.Web.ASPxEditors;
using System.Web.Services;

public partial class PromotionInfo_EditPromotionDetail : Frame.Pages.ActionInfomationEditBasePage
{
    protected override string ActionID
    {
        get { return "-2"; }
    }

    protected override string UserFunctions
    {
        get
        {
            if (CMSUser.Current.IsLogin)
            {
                if (CMSUser.Current.IsAdmin)
                {
                    return "IUDQ";
                }
                else
                {
                    return CMSUser.GetFunction(ActionID) != null ? CMSUser.GetFunction(ActionID).op : "";
                }
            }
            else
            {
                return "";
            }
        }
    }

    protected override bool IsShowSpecialFunctions
    {
        get
        {
            if (CMSUser.Current.IsLogin)
            {
                if (CMSUser.Current.IsAdmin)
                {
                    return true;
                }
                else
                {
                    return CMSUser.GetFunction(ActionID) != null ? (CMSUser.GetFunction(ActionID).op_s == "Y" ? true : false) : false;
                }
            }
            else
            {
                return false;
            }
        }
    }

    protected void LSMDSourceMainProPromotionDetail_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
    {
        string cxbh = "";
        if (Request.QueryString["cxbh"] != null)
        {
            cxbh = Request.QueryString["cxbh"].ToString();
        }
        //string cxbh = "C000000001";
        DataClassesDataContext lqdb = new DataClassesDataContext();
        e.QueryableSource = lqdb.css_sp_mjms_mx.Where(o => o.cxbh == cxbh);

    }

    //ajax客户端获取数据  获取促销明细详细信息
    [WebMethod]
    public static string[] GetPros(string cxbh)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var spbm = context.css_sp_mjms_mx.Where(u => u.cxbh.Trim() == cxbh.Trim()).Select(u => u.spbm).ToArray();
            return spbm;
        }
    }
    protected void gridView_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        string cxbh = "";
        if (Request.QueryString["cxbh"] != null)
        {
            cxbh = Request.QueryString["cxbh"].ToString();
        }
        e.NewValues["cxbh"] = cxbh;
        e.NewValues["founder"] = CMSUser.Current.ID;// CMSUser.Current.Name;
        e.NewValues["fdate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
        e.NewValues["editor"] = CMSUser.Current.ID;// CMSUser.Current.Name;
        e.NewValues["edate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
    }
    protected void gridView_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        e.NewValues["editor"] = CMSUser.Current.ID;// CMSUser.Current.Name;
        e.NewValues["edate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
    }
    //初始化促销编辑单元格
    protected void gridView_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
    {
        if (e.Column.FieldName == "jlxh")
        {
            if (e.Value == null)
            {
                using (DataClassesDataContext lqdb = new DataClassesDataContext())
                {
                    int number = lqdb.css_sp_mjms.Count() + 1;
                    (e.Editor as ASPxTextBox).Text = number.ToString();
                    //WebUtility.GenerateCode(number.ToString(),'C',10);
                }
            }
        }
        if (e.Column.FieldName == "spbm")
        {
            e.Editor.Focus();
        }
        e.Editor.SetClientSideEventHandler("KeyPress", @"function(s,e){var key = event.keyCode;if( key==13){EnterAsTab();}}");
    }
    //添加详细信息
    //保存产品编号   保存促销详情
    protected void ShowProduct_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        using (DataClassesDataContext lqdb = new DataClassesDataContext())
        {
            int index = gridView.FocusedRowIndex;
            if (index == -1)
            {
                index = 0;
            }
            string cxbh = gridView.GetRowValues(index, "cxbh").ToString();
            var result = lqdb.css_sp_mjms_mx.FirstOrDefault(p => p.cxbh == cxbh);
            if (!e.Parameters.Equals(""))
                result.spbm = e.Parameters;
            lqdb.SubmitChanges();

        }
    }

    void databind(string cxbh)
    {
        using (DataClassesDataContext lqdb = new DataClassesDataContext())
        {
            gridView.DataSourceID = "";
            var result = from r in lqdb.css_sp_mjms_mx
                         where r.cxbh == cxbh
                         select r;
            gridView.DataSource = result;
            gridView.KeyFieldName = "jlxh";
            gridView.DataBind();
        }
    }
    
    //生效
    protected void btnSxD_Click(object sender, EventArgs e)
    {
        using (DataClassesDataContext lqdb = new DataClassesDataContext())
        {
            string cxbhs = "";
            if (Request.QueryString["cxbh"] != null)
            {
                cxbhs = Request.QueryString["cxbh"].ToString();
            }
            var result = lqdb.css_sp_mjms_mx.FirstOrDefault(o => o.cxbh == cxbhs);
            result.flag = "Y";//生效标志
            result.sxrq = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");   //生效日期
            lqdb.SubmitChanges();
            databind(cxbhs);
        }

    }
    //取消
    protected void btnQxD_Click(object sender, EventArgs e)
    {
        using (DataClassesDataContext lqdb = new DataClassesDataContext())
        {
            int index = gridView.FocusedRowIndex;
            if (index == -1)
            {
                index = 0;
            }
            string cxbhs = gridView.GetRowValues(index, "cxbh").ToString();

            var result = lqdb.css_sp_mjms_mx.FirstOrDefault(o => o.cxbh == cxbhs);
            result.flag = "N";//生效标志
            result.qxrq = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");   //取消日期
            lqdb.SubmitChanges();
            databind(cxbhs);
        }

    }
}